package Comp355Final.main;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ScrollView;
/**
 * The GUI for image reader
 * @author chung
 *
 */
public class GUIReadImg extends Activity{
    private ImageView img_panel;
    private static Bitmap bm,org_bm;
    private static String dest;
    private int bmp_width,bmp_height,screen_width,screen_height;
    private String charset;
    private String book_name;
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	super.onCreate(savedInstanceState);
    	//Utility.quick_dialog("Image showing", this);
    	Bundle extras = getIntent().getExtras();
    	String tmp_dest = extras.getString("dest");
    	float tmp_scale = extras.getFloat("zoom");
    	book_name = extras.getString("book_name");
    	charset= extras.getString("charset");
    	scale = tmp_scale;
    	//Utility.quick_dialog("Float:"+resize, this);
    	if(dest!=null && !tmp_dest.equals(dest))
    	{
    		bm=null;
    		org_bm=null;
    		img_panel=null;
    	}
    	else if(resize)
    	{
    		Utility.quick_dialog("Zoom: "+String.valueOf(tmp_scale).substring(0, 3)+"X", this);
    		bm=null;
    		img_panel=null;
    		resize=false;
    	}
    	dest=tmp_dest;
    	LinearLayout scrllview = new LinearLayout(this);
    	LinearLayout.LayoutParams scrllview_param = new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.FILL_PARENT,
        		ViewGroup.LayoutParams.FILL_PARENT);
        scrllview.setBackgroundColor(Color.WHITE);
       // scrllview.setFillViewport(true);
    	//LinearLayout hole_panel = new LinearLayout(this);
        //LinearLayout.LayoutParams hole_panel_param = new LinearLayout.LayoutParams(
       // 		ViewGroup.LayoutParams.FILL_PARENT,
        //		ViewGroup.LayoutParams.FILL_PARENT);
        img_panel = new ImageView(this);
        img_panel.setScaleType(ScaleType.CENTER);
    	LayoutParams img_panel_param = new LayoutParams(
        		ViewGroup.LayoutParams.WRAP_CONTENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT);
     //  img_panel.setScrollContainer(true);
   //    img_panel.setScrollBarStyle(img_panel.SCROLLBARS_INSIDE_INSET);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 1;
        if(org_bm==null)
        {
        	org_bm = BitmapFactory.decodeFile(dest, options);
        }
        
        if(bm==null)
        {   
            bm=Utility.resize_drawabletobitmap(org_bm, scale);
            
        }
       
        img_panel.setImageBitmap(bm);
    //    img_panel.getBackground().setCallback(cb)
        int screen_dimension[] = Utility.getdimension(this);

        // set maximum scroll amount (based on center of image)   
        bmp_width = bm.getWidth();
        bmp_height = bm.getHeight();
        screen_width = screen_dimension[0];
        screen_height = screen_dimension[1];
        int maxX = (int)((bmp_width / 2) - (screen_width / 2));
        int maxY = (int)((bmp_height / 2) - (screen_height / 2));
        // set scroll limits
        final int maxLeft = (maxX * -1);
        final int maxRight = maxX; 
        final int maxTop = (maxY * -1);
        final int maxBottom = maxY;
       if(bmp_width>screen_width || bmp_height>screen_height)
        // set touchlistener
        img_panel.setOnTouchListener(new View.OnTouchListener()
        {
        float downX, downY;
        int totalX, totalY;
        int scrollByX, scrollByY;
        
        public boolean onTouch(View view, MotionEvent event)
        {
        	float currentX, currentY;
        	switch (event.getAction())
        	{
        	case MotionEvent.ACTION_DOWN:
        		downX = event.getX();
        		downY = event.getY();
        		break;
        	case MotionEvent.ACTION_MOVE:
        		currentX = event.getX();
        		currentY = event.getY();
        		scrollByX = (int)(downX - currentX);
        		scrollByY = (int)(downY - currentY);
        		// scrolling to left side of image (pic moving to the right)
        		if (currentX > downX)
        		{
        			if (totalX == maxLeft)
        			{
        				scrollByX = 0;
        			}
        			if (totalX > maxLeft)
        			{
        				totalX = totalX + scrollByX;
        			}
        			if (totalX < maxLeft)
        			{
        				scrollByX = maxLeft - (totalX - scrollByX);
        				totalX = maxLeft;
        			}
        		}
        		// scrolling to right side of image (pic moving to the left)
        		if (currentX < downX)
        		{
        			if (totalX == maxRight)
        			{
        				scrollByX = 0;
        			} 
        			if (totalX < maxRight)
        			{ 
        				totalX = totalX + scrollByX;
        			} 
        			if (totalX > maxRight)
        			{
        				scrollByX = maxRight - (totalX - scrollByX);
        				totalX = maxRight;
        			} 
        		}
        		// scrolling to top of image (pic moving to the bottom)
        		if (currentY > downY)
        		{
        			if (totalY == maxTop) 
        			{ 
        				scrollByY = 0; 
        			}
        			if (totalY > maxTop)
        			{
        				totalY = totalY + scrollByY;
        			}
        			if (totalY < maxTop)
        			{ 
        				scrollByY = maxTop - (totalY - scrollByY);
        				totalY = maxTop;
        			}
        		}
        		// scrolling to bottom of image (pic moving to the top)
        		if (currentY < downY)
        		{
        			if (totalY == maxBottom)
        			{
        				scrollByY = 0;
        			}
        			if (totalY < maxBottom)
        			{
        				totalY = totalY + scrollByY; 
        			}
        			if (totalY > maxBottom)
        			{
        				scrollByY = maxBottom - (totalY - scrollByY); 
        				totalY = maxBottom; 
        			} 
        		}  
        		// if(bmp_width>screen_width || bmp_height>screen_height)
        		if(bmp_width<screen_width)
        			scrollByX=0;
        		if(bmp_height<screen_height)
        			scrollByY=0;
        		img_panel.scrollBy(scrollByX, scrollByY);
        		downX = currentX;
        		downY = currentY;
        		break;
        		}
        	return true;
        	}
        });

        this.setTitle("Image Viewer - "+book_name);
        //hole_panel.addView(img_panel,img_panel_param);
        //scrllview.addView(hole_panel,hole_panel_param);
        scrllview.addView(img_panel,img_panel_param);
        this.setContentView(scrllview, scrllview_param);
    }
	@Override
	public void onStop()
	{
		//img_panel.getBackground().setCallback(null);
		//img_panel=null;
		super.onStop();
	}
   private float scale=(float) 1.0000;
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
	   menu.add(0, 0, 0, "Zoom In").setIcon(android.R.drawable.ic_menu_zoom);//
	   menu.add(0, 1, 1, "Zoom Out").setIcon(android.R.drawable.ic_menu_zoom);
	   menu.add(0, 2, 3, "Return to BookShelf").setIcon(android.R.drawable.ic_menu_revert);
	   menu.add(0, 3, 2, "Orginal Size").setIcon(android.R.drawable.ic_menu_report_image );
	   return super.onCreateOptionsMenu(menu); 
   }
	private static boolean resize = false;
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	switch(item.getItemId())
    	{
    	case 0: 
    		scale +=0.2;
			Intent int_ent2 = new Intent(this, GUIReadImg.class);
			int_ent2.putExtra("zoom", scale);
			int_ent2.putExtra("dest", dest);
			int_ent2.putExtra("book_name", this.book_name);
			int_ent2.putExtra("charset", this.charset);
			resize=true;
			clear_me();
			this.startActivity(int_ent2);

			this.finish();
			
    		break;
    	case 1: 
    		scale -=0.2;
			Intent int_ent3 = new Intent(this, GUIReadImg.class);
			int_ent3.putExtra("zoom", scale);
			int_ent3.putExtra("dest", dest);
			int_ent3.putExtra("book_name", this.book_name);
			int_ent3.putExtra("charset", this.charset);

			resize=true;
			clear_me();
			this.startActivity(int_ent3);
			
			this.finish();
    		break;
    	case 2: 
			Intent int_ent = new Intent(this, GUIBookShelfBkSelect.class);
			
			
			this.startActivity(int_ent);
			clear_me();
			this.finish();
			
    		break;
    	case 3: 
    		scale =1;
			Intent int_ent4 = new Intent(this, GUIReadImg.class);
			int_ent4.putExtra("zoom", scale);
			int_ent4.putExtra("dest", dest);
			int_ent4.putExtra("book_name", this.book_name);
			int_ent4.putExtra("charset", this.charset);
			resize=true;
			clear_me();
			this.startActivity(int_ent4);
			this.finish();
    		break;
    	}
    	return super.onOptionsItemSelected(item);
    }
   private void clear_me()
   {
	   this.charset=null;
	   this.img_panel=null;
	   this.bm=null;
	   this.org_bm=null;
	   this.dest=null;
	   this.book_name=null;
   }

}
